package com.cloud.report.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.cloud.report.api.entity.ReportDataview;
import com.cloud.report.api.entity.ReportDataviewField;
import com.cloud.report.api.entity.ReportQuery;
import com.cloud.report.api.mapper.ReportDataviewFieldMapper;
import com.cloud.report.api.mapper.ReportDataviewMapper;
import com.cloud.report.api.mapper.ReportQueryMapper;
import com.cloud.report.api.service.ReportDataViewService;
import com.cloud.report.common.dynamic.DynamicDataSource;
import com.cloud.report.common.exception.BizException;
import com.cloud.report.common.result.QkIcon;
import com.cloud.report.common.result.QkTreeNode;
import com.cloud.report.common.utils.RedisKeyHelper;
import com.cloud.report.common.utils.TreeHelper;

import tk.mybatis.mapper.entity.Example;

@Service
@Transactional
public class ReportDataviewServiceImpl implements ReportDataViewService {

	@Autowired
	private ReportDataviewMapper reportDataviewMapper;

	@Autowired
	private ReportDataviewFieldMapper reportDataviewFieldMapper;

	@Autowired
	private ReportQueryMapper reportQueryMapper;

	@Autowired
	private JdbcTemplate jdbcTemplate;

	@Override
	public QkTreeNode[] listDataViewTree() {
		List<ReportDataview> dataViews = reportDataviewMapper.selectAll();
		List<QkTreeNode> nodes = new ArrayList<QkTreeNode>();
		Map<String, QkTreeNode> temp = new HashMap<String, QkTreeNode>();
		for (ReportDataview view : dataViews) {
			QkTreeNode grpNode = null;
			if (temp.containsKey(view.getGroupName())) {
				grpNode = temp.get(view.getGroupName());
			} else {
				grpNode = new QkTreeNode(view.getGroupName(), view.getGroupName());
				grpNode.setRootFlag(true);
				grpNode.setExpanded(true);
				grpNode.setIcon(QkIcon.FA_FOLDER);
				nodes.add(grpNode);
			}
			QkTreeNode dataviewNode = new QkTreeNode(view.getDataviewId(), view.getDataviewName());
			dataviewNode.setLeaf(true);
			dataviewNode.setSelectable(true);
			dataviewNode.setSeniorId(grpNode.getId());
			dataviewNode.setIcon(QkIcon.FA_TABLE);
			nodes.add(dataviewNode);
		}
		return TreeHelper.buildList2Tree(nodes);
	}

	@Override
	public void deleteDataview(long id) {
		Example queryExample = new Example(ReportQuery.class);
		Example.Criteria queryCriteria = queryExample.createCriteria();
		queryCriteria.andEqualTo("dataviewId", id);
		int count = reportQueryMapper.selectCountByExample(queryExample);
		if (count > 0) {
			throw new BizException("视图被统计查询引用，不能被删除");
		}
		// 刪除数据视图对应的字段
		Example dataviewFieldExample = new Example(ReportDataviewField.class);
		Example.Criteria dataviewFieldCriteria = dataviewFieldExample.createCriteria();
		dataviewFieldCriteria.andEqualTo("dataviewId", id);
		reportDataviewFieldMapper.deleteByExample(dataviewFieldExample);
		// 删除数据视图
		reportDataviewMapper.deleteByPrimaryKey(id);
	}

	@Override
	public ReportDataview showDataviewDetail(long id) {
		ReportDataview dataView = reportDataviewMapper.selectByPrimaryKey(id);
		Example dataviewFieldExample = new Example(ReportDataviewField.class);
		Example.Criteria dataviewFieldCriteria = dataviewFieldExample.createCriteria();
		dataviewFieldCriteria.andEqualTo("dataviewId", id);
		List<ReportDataviewField> fields = reportDataviewFieldMapper.selectByExample(dataviewFieldExample);
		dataView.setDataviewFields(fields);
		return dataView;
	}

	@Override
	public ReportDataview saveUpdateDataview(ReportDataview dataview) {
		Long dataviewId = dataview.getDataviewId();
		if (null == dataviewId || dataviewId <= 0) {
			dataviewId = RedisKeyHelper.genereateKey(ReportDataview.class);
			dataview.setDataviewId(dataviewId);
			reportDataviewMapper.insert(dataview);
		} else {
			reportDataviewMapper.updateByPrimaryKey(dataview);
		}
		return dataview;
	}

	@Override
	public int saveUpdateDataviewFields(long id, List<ReportDataviewField> fields) {
		Example fieldExample = new Example(ReportDataviewField.class);
		Example.Criteria fieldCriteria = fieldExample.createCriteria();
		fieldCriteria.andEqualTo("dataviewId", id);
		reportDataviewFieldMapper.deleteByExample(fieldExample);

		for (ReportDataviewField field : fields) {
			long fieldId = RedisKeyHelper.genereateKey(ReportDataviewField.class);
			field.setFieldId(fieldId);
			field.setDataviewId(id);
			reportDataviewFieldMapper.insert(field);
		}
		return fields.size();
	}

	@Override
	@DynamicDataSource
	public List<String> listTableViews(String dsName) {
		// 暂时只支持MySQL
		return jdbcTemplate.queryForList("show tables", String.class);
	}
}
